<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models\TestApi;

use Carbon\Carbon;
use App\Models\Eloquent;
use App\Models\Meimei\Traits\SignOssUrlTrait;
use App\Models\Meimei\Traits\ExplodeTrait;

/**
 * Class QtpTestReport
 *
 * @property int $id
 * @property string $test_report_uid
 * @property string $title
 * @property int $test_api_sun
 * @property int $test_param_sun
 * @property int $success_param_sun
 * @property int $duration
 * @property int $status
 * @property string $test_adminer
 * @property string $created_adminer
 * @property string|null $remark
 * @property string|null $explain
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 * @property Carbon|null $tested_at
 *
 * @package App\Models
 */
class TestReport extends Eloquent
{

    use ExplodeTrait;
    use SignOssUrlTrait;
    protected $explode = ['explain'];
	protected $table = 'test_reports';

	protected $casts = [
		'test_api_sun' => 'int',
		'test_param_sun' => 'int',
		'success_param_sun' => 'int',
		'duration' => 'int',
        'status' => 'int'
	];

	protected $fillable = [
		'test_report_uid',
		'title',
		'test_api_sun',
		'test_param_sun',
		'success_param_sun',
		'duration',
		'test_adminer',
        'tested_at',
        'created_adminer',
		'remark',
		'explain',
        'status'
	];

    // 0待执行 1执行完成
    const STATUS_INIT = 0;
    const STATUS_DONE = 1;
    public static $status_maps = [
        self::STATUS_INIT => '待执行',
        self::STATUS_DONE => '执行完成'
    ];

    public function label()
    {
        $this->status_label = self::$status_maps[$this->status] ?? '';
        $this->passing_rate = 0;
        if($this->success_param_sun > 0) {
            $this->passing_rate = round($this->success_param_sun/$this->test_param_sun, 4) * 100;
        }

        $this->test_validator_sum = TestValidatorRes::where('test_report_uid', $this->test_report_uid)->count();
        $this->success_validator_sum = TestValidatorRes::where('status', TestValidatorRes::STATUS_SUCCESS)->where('test_report_uid', $this->test_report_uid)->count();

        return $this;
    }

    public static $fillables = [
        'test_report_uid' => '测试报告唯一UUID',
        'title' => '测试报告标题',
        'test_api_sun' => '测试接口总数量',
        'test_param_sun' => '测试参数(用例)总数量',
        'success_param_sun' => '测试参数(用例)成功总数量',
        'duration' => '用例运行总时长',
        'test_adminer' => '执行测试人',
        'created_adminer' => '测试单创建人',
        'remark' => '测试单说明',
        'explain' => '测试单记录 开发组，项目，产品，主模块，子模块等',
        'status' => '测试单运行状态 0待跑 1已跑'
    ];

    public static function getByUid(string $test_report_uid): ?self
    {
        return self::where('test_report_uid', $test_report_uid)->first();
    }
}
